﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;

namespace EIBDDownloader
{
    class Program
    {
        static bool finishedDownloading = false;

        const string PATTERN = @"http://www\.investors\.com/pdf/(?<issue>eIBD\d{6})\.pdf";

        static void Main(string[] args)
        {
            #region Web lookup variables

            HttpWebRequest webRequest;
            HttpWebResponse webResponse;
            string pageSource = string.Empty;
            CookieContainer cookieContainer = new CookieContainer();

            #endregion

            #region Login to get cookies

            Console.WriteLine("Logging in to get cookies");

            string loginUrl = "http://www.investors.com/Services/SiteAjaxService.asmx/MemberSingIn?strEmail=ssattin@gmail.com&strPassword=dennis10&blnRemember=false";

            webRequest = (HttpWebRequest)(WebRequest.Create(loginUrl));
            webRequest.CookieContainer = cookieContainer;
            webRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, #/#";
            webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5";
            webRequest.KeepAlive = true;
            webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            webRequest.Method = "GET";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webResponse = (HttpWebResponse)webRequest.GetResponse();

            //get the response details
            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                //get the page response
                pageSource = streamReader.ReadToEnd();

                //write the response to diagnostic file
                using (StreamWriter writer = new StreamWriter(new FileStream("testlogin.xml", FileMode.Create)))
                {
                    writer.Write(pageSource);
                }
            }

            webResponse.Close();

            Console.WriteLine(pageSource);

            #endregion

            #region Find issues

            webRequest = (HttpWebRequest)(WebRequest.Create("http://www.investors.com/MyIBD/RecentIssues.aspx"));
            webRequest.CookieContainer = cookieContainer;
            webRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, #/#";
            webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5";
            webRequest.KeepAlive = true;
            webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            webRequest.Method = "GET";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webResponse = (HttpWebResponse)webRequest.GetResponse();

            //get the response details
            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                //get the page response
                pageSource = streamReader.ReadToEnd();                
            }

            webResponse.Close();

            #endregion

            CookieAwareWebClient client = new CookieAwareWebClient();

            client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

            client.Cookies = cookieContainer;

            RegexOptions options = RegexOptions.Singleline;

            string input = pageSource.Replace("\r", "").Replace("\n", "").Replace("\t", "");

            MatchCollection matches = Regex.Matches(pageSource, PATTERN, options);
            foreach (Match match in matches)
            {
                string filename = string.Format(@"C:\Data\IBD\EIBD\{0}.pdf", match.Groups["issue"].Value);

                if (!File.Exists(filename))
                {
                    string fromFileName = match.Value;

                    finishedDownloading = false;

                    client.DownloadFileAsync(new Uri(fromFileName), filename);

                    //block until finished
                    while (!finishedDownloading)
                    {
                        Thread.Sleep(1000);
                    }
                }

                Console.WriteLine(match.Value);
            }

            //File.WriteAllBytes("eIBD082211.pdf", resource);            

            webResponse.Close();

            
            
            Console.WriteLine("Done");
        }

        static void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            Console.WriteLine("Download completed");
            if (e.Error != null)
            {
                Console.WriteLine(e.Error);   
            }            

            finishedDownloading = true;
        }

        static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Console.Write("\r" + e.ProgressPercentage.ToString());
        }     
    }
}
